package br.com.android.pubclub.dados;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import br.com.android.pubclub.basicas.Bebida;
import br.com.android.pubclub.basicas.Medida;
import br.com.android.pubclub.basicas.Saldo;
import br.com.android.pubclub.web.Login;

public class ThreadPresentearMeuSaldo extends Thread {
	
	int action;
	Bundle parametros;
	Handler hand;
	public static final int MEU_SALDO = 0;
	public static final int PRESENTEAR = 1; 
	

	public ThreadPresentearMeuSaldo(Handler hand, int action, Bundle parametros) {

		this.action = action;
		this.parametros = parametros;
		this.hand = hand;

	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		Message mensagem = new Message();

		Bundle retorno = new Bundle();

		mensagem.what = ThreadAmizadesAcoes.EXECUTANDO;

		hand.sendMessage(mensagem);

		Message msg = new Message();

		switch (action) {
		
		case MEU_SALDO:
			
			retorno.putSerializable("meuSaldo", getSaldoCliente(Integer.valueOf(Login.clienteLogado.getId_cliente())));
			
			break;
		case PRESENTEAR:
			
			retorno = presentearAmigo(Integer.valueOf(Login.clienteLogado.getId_cliente()), parametros.getInt("clienteFavorecidoId"), parametros.getString("listaDeProdutos"));
			
			break;
		
		}
		msg.setData(retorno);
		
		msg.what = ThreadAmizadesAcoes.EXIBINDO;

		hand.sendMessage(msg);
		
		Message ms = new Message();
		
		
		ms.what = ThreadAmizadesAcoes.CONCLUIDO;
		
		try {
			sleep(2000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		hand.sendMessage(ms);
	}

	private Bundle presentearAmigo(int clienteId, int clienteFavorecidoId,
			String listaDeProdutos) {

		Bundle bd = new Bundle();

		SoapObject resposta = null;

		SoapObject soap = new SoapObject("http://www.outsystems.com",
				"Presente");

		soap.addProperty("ClienteId", clienteId);
		soap.addProperty("FavorecidoId", clienteFavorecidoId);
		soap.addProperty("ListaDeProdutos", listaDeProdutos);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);

		envelope.dotNet = true;
		envelope.implicitTypes = true;
		envelope.setOutputSoapObject(soap);

		String url = "http://flplucio.outsystemscloud.com/PubClubCore/PubClub_Presente_WS.asmx";

		HttpTransportSE httpTransport = new HttpTransportSE(url);

		try {

			httpTransport
					.call("http://PubClubCore/PubClub_Presente_WS/Presente",
							envelope);

			resposta = (SoapObject) envelope.getResponse();

			bd.putString("mensagem", resposta.getProperty("Menssagem")
					.toString());
			bd.putBoolean("sucesso",
					Boolean.valueOf(resposta.getProperty("Sucesso").toString()));
			bd.putInt("debitoId", Integer.valueOf(resposta.getProperty(
					"DebitoId").toString()));
			bd.putInt("pedidoId", Integer.valueOf(resposta.getProperty(
					"PedidoId").toString()));

		} catch (Exception e) {

			e.printStackTrace();
		}

		return bd;
	}

	private Bebida getBebida(int bebidaId) {

		SoapObject resposta = null;

		Bebida bebida = new Bebida();

		SoapObject soap = new SoapObject("http://www.outsystems.com",
				"GetBebidaById");

		soap.addProperty("BebidaId", bebidaId);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);

		envelope.dotNet = true;
		envelope.implicitTypes = true;
		envelope.setOutputSoapObject(soap);

		String url = "http://flplucio.outsystemscloud.com/pubclubcore/Utilizadades_WS.asmx";

		HttpTransportSE httpTransport = new HttpTransportSE(url);

		try {

			httpTransport.call(
					"http://PubClubCore/Utilizadades_WS/GetBebidaById",
					envelope);

			resposta = (SoapObject) envelope.getResponse();

			bebida.setDescricao(resposta.getProperty("Descricao").toString());
			bebida.setId(bebidaId);
			bebida.setMedida(getMedida(Integer.valueOf(resposta.getProperty(
					"MedidaId").toString())));
			bebida.setValor(Float.valueOf(resposta.getProperty("Valor")
					.toString()));

		} catch (Exception e) {

			e.printStackTrace();
		}

		return bebida;

	}

	private Medida getMedida(int medidaId) {

		SoapObject resposta = null;

		Medida medida = new Medida();

		SoapObject soap = new SoapObject("http://www.outsystems.com",
				"GetMedidaById");

		soap.addProperty("MedidaId", medidaId);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);

		envelope.dotNet = true;
		envelope.implicitTypes = true;
		envelope.setOutputSoapObject(soap);

		String url = "http://flplucio.outsystemscloud.com/pubclubcore/Utilizadades_WS.asmx";

		HttpTransportSE httpTransport = new HttpTransportSE(url);

		try {

			httpTransport.call(
					"http://PubClubCore/Utilizadades_WS/GetMedidaById",
					envelope);

			resposta = (SoapObject) envelope.getResponse();

			medida.setDescricao(resposta.getProperty("Descricao").toString());
			medida.setId(medidaId);
			medida.setVolume(Integer.valueOf(resposta.getProperty("Volume")
					.toString()));

		} catch (Exception e) {

			e.printStackTrace();
		}

		return medida;

	}

	private ArrayList<Saldo> getSaldoCliente(int clienteId) {

		SoapObject resposta = null;

		ArrayList<Saldo> saldoGeral = new ArrayList<Saldo>();

		SoapObject soap = new SoapObject("http://www.outsystems.com",
				"SaldoCliente");

		soap.addProperty("ClienteId", clienteId);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);

		envelope.dotNet = true;
		envelope.implicitTypes = true;
		envelope.setOutputSoapObject(soap);

		String url = "http://flplucio.outsystemscloud.com/PubClubCore/PubClub_Relatorio_WS.asmx";

		HttpTransportSE httpTransport = new HttpTransportSE(url);

		try {

			httpTransport.call(
					"http://PubClubCore/PubClub_Relatorio_WS/SaldoCliente",
					envelope);

			resposta = (SoapObject) envelope.getResponse();

			for (int ind = 0; ind < resposta.getPropertyCount(); ind++) {

				Saldo saldo = new Saldo();

				SoapObject soapTempObj = (SoapObject) resposta.getProperty(ind);

				saldo.setBedida(getBebida(Integer.valueOf(soapTempObj
						.getProperty("BebidaId").toString())));
				saldo.setCreditos(Integer.valueOf(soapTempObj.getProperty(
						"Creditos").toString()));
				saldo.setDebitos(Integer.valueOf(soapTempObj.getProperty(
						"Debitos").toString()));
				saldo.setTotal(Integer.valueOf(soapTempObj.getProperty(
						"Resultado").toString()));
				saldoGeral.add(saldo);
			}

		} catch (Exception e) {

			e.printStackTrace();
		}

		return saldoGeral;

	}

}
